﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace QLTV.HeThongXuLy
{
    class SachDB
    {
        public static DataTable LayDanhSachSach()
        {
            string sql = "Select * from SACH";
            return ConnectDB.ExecQuery(sql);
        }
        public static DataTable LayMaTuDong()
        {
            string sql = "Select MaSach from SACH ORDER BY MaSach DESC";
            return ConnectDB.ExecQuery(sql);
        }
        public static bool ThemSach(Sach Sach)
        {
            try
            {
                string sql =
                    "INSERT INTO SACH VALUES ('" + Sach.MaSach + "','"
                    + Sach.TenSach + "','"
                    + Sach.TacGia + "','"
                    + Sach.NamXuatBan + "','"
                    + Sach.NamXuatBan + "','"
                    + Sach.TriGia + "','"
                    + Sach.MaNhanVien + "','"
                    + Sach.MaTheLoai + "','"
                    + Sach.SoLuong + "')";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
               
                return false;

            }

        }

        public static bool SuaSach(Sach sach)
        {
            try
            {
                string sql =
                    "UPDATE SACH SET TenSach='" + sach.TenSach +
                    "', TacGia='" + sach.TacGia +
                    "', NamXuatBan='" + sach.NamXuatBan +
                    "', NhaXuatBan='" + sach.NhaXuatBan +
                    "', TriGia='" + sach.TriGia +
                    "',MaNhanVien='" + sach.MaNhanVien +
                    "', MaTheLoai='" + sach.MaTheLoai +
                    "', SoLuong='" + sach.SoLuong +
                    "' where MaSach='" + sach.MaSach + "'";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {
                
                return false;

            }

        }

        public static bool XoaSach(Sach Sach)
        {
            try
            {
                string sql =
                    "Delete from SACH where MaSach='" + Sach.MaSach + "'";

                ConnectDB.ExecNonQuery(sql);

                return true;
            }
            catch (Exception ex)
            {                
                return false;

            }

        }
        public static DataTable DanhSachTheLoai()
        {
            string sql = "Select * from THELOAISACH";
            return ConnectDB.ExecQuery(sql);
        }

        public static String TimTenSach(string Ma)
        {
            string sql = "select TenSach from SACH where MaSach = '" + Ma + "'";
            DataTable dt = ConnectDB.ExecQuery(sql);
            if (dt.Rows.Count != 0)
            {
                return dt.Rows[0][0].ToString();
            }
            else
            {
                return "";
            }
        }
    }
}
